package sim;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Stack;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import matrics.SparseVector;
import myLucene.GetTime;
import myLucene.Props;
import prepro.SerializeStopWordObject;
import prepro.SerializeTermsToInt;
import tWeight.TfIdf;

public class MakeTFObjects {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String windowSize ="6";
		String pathname = "/local/joofeit/backAll_"+windowSize+"/TF";
		File file = new File(pathname);
		//String[] intToTerms = SerializeTermsToInt.deserializeArray();
		Directory iDirectory = FSDirectory.open(new File(Props.readIndexPath));
		IndexSearcher searcher = new IndexSearcher(iDirectory, true);
		IndexReader iReader = searcher.getIndexReader();
		if (file.isDirectory()) {
			File[] fList = file.listFiles();
			for (File curFile : fList) {
				System.out.println("!!!!!!!!1"+curFile.getAbsolutePath());
				int curInt = Integer.parseInt(curFile.getAbsolutePath().split(
						"/")[5].split("_")[0]);
				String curTerm = curFile.getAbsolutePath().split("/")[5]
						.split("_")[1];

				
//				if (!curTerm.equals(intToTerms[curInt])) {
//					throw new Exception(
//							"Severe Error: !curTerm.equals(intToTerms[curInt])");
//				}
				System.out.println(curTerm);
				System.out.println(curFile.getAbsolutePath());

				int[] array = ReadObject.readArray(curFile.getAbsolutePath());
				Stack<Integer> stops = SerializeStopWordObject
						.deserializeStops();

				SparseVector sp = TfIdf.getTfIdf(array, iReader, stops);
				ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
						"/local/joofeit/TFIDF_"+windowSize+"/TFIDF_"
								+ curFile.getAbsolutePath().split("/")[5]));
				out.writeObject(sp);
				out.close();
				long start = System.currentTimeMillis();
//				File file2 = new File("/local/joofeit/TFIDF_20/TFIDF_"
//						+ curFile.getAbsolutePath().split("/")[5]);
//				ObjectInputStream in = new ObjectInputStream(
//						new FileInputStream(file2));
//				
//				// Deserialize the object
//				SparseVector sp_in = (SparseVector) in.readObject();
//				System.out.println("TEST: "+sp_in.get(1));
//				in.close();
				System.out
						.println("Time needed for deserialization of TFIDF: "
								+ GetTime.getDuration(start, System
										.currentTimeMillis()));

				// writeToFileArray(array, intToTerms);

			}
		}
		// TODO Auto-generated method stub

	}

}
